package com.jiuyun.dao.impl;

import com.jiuyun.dao.StudentDao;
import com.jiuyun.entity.Student;
import com.jiuyun.until.AliPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    QueryRunner runner = new QueryRunner();
    Connection conn = null;
    @Override
    public List getStu() throws SQLException {
        String sql = "select * from student";
        conn = AliPool.getConn();
//        List list = new ArrayList();
//        //原生sql查询方法
//        PreparedStatement pst =conn.prepareStatement(sql);
//        ResultSet  rs = pst.executeQuery();
//        while (rs.next()){
//            Student st = new Student();
//            st.setStid(rs.getInt(1));
//            st.setClassid(rs.getInt(2));
//            st.setStname(rs.getString(3));
//            st.setStpass(rs.getString(4));
//            list.add(st);
//        }
//        conn.close();

        //QueryRunner简洁查询方法
        List<Student> list = runner.query(conn,sql,new BeanListHandler<Student>(Student.class));
        conn.close();
        return list;
    }
}
